package com.rfid.emergencywarehouse.fragment;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.rfid.base.util.AnimatorHelper;
import com.rfid.emergencywarehouse.BarcodeScanner;
import com.rfid.emergencywarehouse.R;
import com.rfid.emergencywarehouse.adapter.GetAssetAdapter;
import com.rfid.emergencywarehouse.adapter.PandianAdapter;
import com.rfid.emergencywarehouse.entity.BarcodeInfo;
import com.rfid.emergencywarehouse.presenter.GetAssetPresenter;
import com.rfid.emergencywarehouse.presenter.PanDianPresenter;
import com.rfid.emergencywarehouse.view.IGetAssetView;
import com.rfid.emergencywarehouse.view.IPandianvView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PanDianFragment  extends BaseScanListFragment<PandianAdapter, BarcodeInfo, PanDianPresenter> implements IPandianvView, BarcodeScanner.Listener {


    @BindView(R.id.barcode)
    EditText mBarcodeEdit;
    @BindView(R.id.kwbarcode)
    EditText mKWBarcodeEdit;
    BarcodeScanner mScanner;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this,view);
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mScanner = new BarcodeScanner(context,this);
        mScanner.listenBarcode(context);
    }

    @Override
    public void onDetach() {
        mScanner.releaseListenBarcode(getContext());
        super.onDetach();
    }

    @Override
    protected int getScanRfidCheckBoxId() {
        return R.id.scan;
    }

    @Override
    protected EditText getBarcodeEdit() {
        return mBarcodeEdit;
    }

    @Override
    protected PandianAdapter getAdapter() {
        return new PandianAdapter(mData);
    }

    @Override
    public PanDianPresenter getPresenter() {
        return new PanDianPresenter();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_pandian;
    }

    @Override
    public void onPandianResult(List<BarcodeInfo> barcodeInfos) {
        mData.clear();
        mData.addAll(barcodeInfos);
        mAdapter.setSelectable(false);
        mAdapter.notifyDataSetChanged();
        mSelects.clear();
    }

    @Override
    protected void onStartScan() {
        super.onStartScan();
        mAdapter.setSelectable(true);
    }

    @Override
    public void onFailMsg(String msg) {
        showTip(msg);
    }

    @Override
    public void onScanBarcode(String barcode) {
        mKWBarcodeEdit.setText(barcode);
    }

    @OnClick(R.id.add)
    public void add(View v){
        String shelf = mKWBarcodeEdit.getText().toString();
        if (TextUtils.isEmpty(shelf) && mSelects.size() == 0) {
            AnimatorHelper.shake(getContext(),mKWBarcodeEdit);
            return;
        }
        List<String> barcodes = getBarcodes();
        if (barcodes == null) {
            return;
        }
        mPresenter.pandian(shelf,barcodes);
    }
}
